package com.yand.controller.business.order;


import com.github.pagehelper.PageInfo;
import com.yand.api.entity.business.custom.THfStudent;
import com.yand.api.entity.business.order.THfOrder;
import com.yand.api.entity.business.product.THfDiscountDetail;
import com.yand.api.entity.business.product.THfProductDetail;
import com.yand.api.entity.common.Result;
import com.yand.api.service.business.DiscountService;
import com.yand.api.service.business.ProductService;
import com.yand.api.service.business.custom.StudentService;
import com.yand.api.service.business.order.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.Date;

/**
 *  订单控制器类
 *
 * @author  yanzz
 */
@Api(tags = "订单控制器类")
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService service;

    @Autowired
    private StudentService studentService;

    @Autowired
    private DiscountService discountService;

    @Autowired
    private ProductService productService;


    @ApiOperation("跳转订单列表页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "订单明细", required = true)
    })
    @GetMapping("/list")
    public ModelAndView list(THfOrder order, Model model){
        ModelAndView modelAndView = new  ModelAndView();

        modelAndView.setViewName("business/order/order-list");
        PageInfo<THfOrder> orderPageInfo = service.queryAllOrderList(order);
        model.addAttribute("pageInfo",orderPageInfo);

        model.addAttribute("order",order);

        return modelAndView;
    }

    @ApiOperation("添加订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "为学生添加订单", required = true)
    })
    @GetMapping("/add/{studentId}")
    public ModelAndView addOrder(@PathVariable(value = "studentId") Long studentId,Model model){
        ModelAndView modelAndView = new  ModelAndView();
        modelAndView.setViewName("business/order/order-add");
        THfStudent student = studentService.getStudentBean(studentId);
        /**
         * 查询当前年级下的A级课程的价格
         */
        String level = service.convertProductLevel(String.valueOf(student.getGrade()));
        THfProductDetail detail = productService.queryProductPrice(level, "1");
        model.addAttribute("student",student);
        model.addAttribute("price",detail.getPrice());

        return modelAndView;
    }


    /**
     * 根据缴费金额获取折扣
     * @param payMoney
     * @return
     */
    @ApiOperation("根据缴费金额获取折扣")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "根据缴费金额获取折扣", required = true)
    })
    @PostMapping("/discount")
    public THfDiscountDetail getDiscount(@RequestParam("payMoney") String payMoney ){


         return discountService.queryDiscountByPayMoney(payMoney);
    }


    /**
     * 根据年级与订单类型获取单价
     * @return
     */
    @ApiOperation("根据年级与订单类型获取单价")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "根据年级与订单类型获取单价", required = true)
    })
    @PostMapping("/price")
    public THfProductDetail getProductPrice(@RequestParam("grade") String grade, @RequestParam("orderType") String orderType){

        /**
         * 将年级转换为产品等级
         */
        String level = service.convertProductLevel(grade);


        THfProductDetail detail = productService.queryProductPrice(level, orderType);

        return detail;

    }


    /**
     * 将前台传过来的订单信息入库
     * @return
     */
    @ApiOperation("绑定订单入库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "order", value = "绑定订单入库", required = true)
    })
    @PostMapping("/add")
    public Result addOrder(THfOrder order){
        //订单入库逻辑
        service.insertToOrder(order);
        //把学生表的订单日期添加上以及将学生表的订单状态改为订单已绑定
        THfStudent student = new THfStudent();
        student.setId(order.getStudentId());
        student.setCreateTime(new Date());
        student.setExtand1("1");
        studentService.updateStudent(student);
        return new Result(0);
    }







}
